package com.audaque.springboot.foshanupload.web.streamdemo.annoMode.listener;


import com.audaque.springboot.foshanupload.web.streamdemo.annoMode.channel.TestChannel;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.messaging.handler.annotation.Header;

//新的写法与旧的是不兼容的，源码在启动时会校验 bean 上下文是否有标注 @EnableBinding 的类，如果有则函数式的定义不会生效。
//@EnableBinding(TestChannel.class)
public class ReceiveMessageListener {


  @StreamListener(Sink.INPUT)
  public void input(Message<String> message,
                    @Header(name = AmqpHeaders.CHANNEL) Channel channel,
                    @Header(name = AmqpHeaders.DELIVERY_TAG) Long deliveryTag) throws Exception {
    System.out.println("消费者1号，------->接收到的消息：" + message.getPayload());
    channel.basicAck(deliveryTag, false);
  }

  @StreamListener(TestChannel.TEST_INPUT)
  public void testConsumeMessage(Message<String> message,
                                 @Header(name = AmqpHeaders.CHANNEL) Channel channel,
                                 @Header(name = AmqpHeaders.DELIVERY_TAG) Long deliveryTag) throws Exception {

    System.err.println("消费者消费消息：" + message.getPayload());
    channel.basicAck(deliveryTag, false);
  }


}
